package com.redare.dev.smart.generator.workbench.controller;

import com.redare.devframework.common.pojo.CommonResult;
import com.redare.devframework.common.pojo.Page;
import com.redare.devframework.webplatform.sdk.core.AccountSdk;
import com.redare.devframework.webplatform.sdk.core.PlatformSdk;
import com.redare.devframework.webplatform.sdk.core.UserSdk;
import com.redare.devframework.webplatform.sdk.core.arg.AccountArg;
import com.redare.devframework.webplatform.sdk.core.arg.RoleArg;
import com.redare.devframework.webplatform.sdk.core.arg.UserArg;
import com.redare.devframework.webplatform.sdk.core.pojo.PlatformRole;
import com.redare.devframework.webplatform.sdk.core.pojo.User;
import com.redare.devframework.webplatform.workbench.conf.annotations.WebFun;
import com.redare.devframework.webplatform.workbench.controller.component.BaseWebController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author hxw
 */
@RequestMapping(value = "/api/user")
@Controller
public class UserController extends BaseWebController {

    @Autowired
    UserSdk userSdk;

    @Autowired
    AccountSdk accountSdk;

    @Autowired
    PlatformSdk platformSdk;


    @ResponseBody
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public CommonResult<Page<User>> findUserPage(
            @RequestBody UserArg.Search form,
            @RequestParam(value = "curPage", required = false, defaultValue = "1") int curPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        //获取用户信息
        return userSdk.findUserPage(form, curPage, pageSize);
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @WebFun(classifyName = "用户成员管理", id = "addUser", name = "新增用户")
    public CommonResult addUser(
            @RequestBody UserArg.Add form
    ) {
        form.setPlatform("root");
        return userSdk.addUser(form);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @WebFun(classifyName = "用户成员管理", id = "editUser", name = "编辑用户")
    public CommonResult<Void> editUser(
            @RequestBody UserArg.Edit form
    ) {
        return userSdk.modifyUser(form);
    }

    @ResponseBody
    @RequestMapping(value = "/disableOrEnable", method = RequestMethod.POST)
    @WebFun(classifyName = "用户成员管理", id = "disableOrEnableUser", name = "启用、禁用用户")
    public CommonResult<Void> disableOrEnableUser(
            @RequestBody AccountArg.Status form
    ) {
        return accountSdk.modifyStatus(form);
    }

    @ResponseBody
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public CommonResult<User> getUser(
            @RequestBody AccountArg.Get form
    ) {
        form.setIncRole(true);
        return userSdk.getUser(form);
    }

    @ResponseBody
    @RequestMapping(value = "/pwd", method = RequestMethod.POST)
    @WebFun(classifyName = "用户成员管理", id = "updateUserPwd", name = "修改密码")
    public CommonResult<Void> updateUserPwd(
            @RequestBody AccountArg.Pwd form
    ) {
        return accountSdk.modifyPwd(form);
    }

    @ResponseBody
    @RequestMapping(value = "/role", method = RequestMethod.POST)
    @WebFun(classifyName = "用户成员管理", id = "updateUserRole", name = "用户角色修改")
    public CommonResult updateUserRole(
            @RequestBody RoleArg.Modify form
    ) {
        return accountSdk.modifyRole(form);
    }


    @ResponseBody
    @RequestMapping(value = "/role/list", method = RequestMethod.POST)
    public CommonResult<List<PlatformRole>> findRoleList(
            @RequestBody com.redare.devframework.webplatform.sdk.core.arg.PlatformRoleArg.Platform form,
            HttpServletRequest request
    ) {
        User loginUserInfo = this.getLoginUserInfo(request);
        form.setPlatform(loginUserInfo.getPlatform());
        return this.platformSdk.findRoleList(form);
    }

    @ResponseBody
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @WebFun(classifyName = "用户成员管理", id = "delUser", name = "删除用户")
    public CommonResult delUser(
            @RequestBody AccountArg.Del form,
            HttpServletRequest request
    ) {
        User user = this.getLoginUserInfo(request);
        if (user==null||user.getUserId()==null){
            return CommonResult.returnFailsWrapper("登录信息失效");
        }
        if (user.getUserId().equals(form.getAccountId())){
            return CommonResult.returnFailsWrapper("不能删除自己的账号");
        }
        return accountSdk.delAccount(form);
    }


}
